# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

first = ListNode()
first.val = 1
second = ListNode()
second.val = 2
third = ListNode()
third.val = 3
fourth = ListNode()
fourth.val = 4
fifth = ListNode()
fifth.val = 5
first.next = second
second.next = third
third.next = fourth
fourth.next = fifth

class Solution(object):
    def reverseList(self, head):
        if head:
            p = head
            head = None
            while p:
                r = p.next
                p.next = head
                head = p
                p = r
        return head

if __name__ == '__main__':
    s = Solution()
    r = s.reverseList(first)
    while r:
        print(r.val)
        r = r.next